package com.FH.applyForm.officeServies.export.meeting;

import com.FH.applyForm.officeServies.export.notice.ExportNoticeShowDTO;
import com.FH.util.AccessDB;
import com.FH.util.ExportExcel;
import com.FH.util.ExportPDF;
import com.FH.util.ResultDTO;
import com.itextpdf.text.DocumentException;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Stateless
public class ExportMeetingDAOImpl extends AccessDB implements ExportMeetingDAO {

    @Override
    public ResultDTO exportMeeting(String condition) {

        ResultDTO resultDTO = new ResultDTO();
        String downloadUrl = "";

        String pdfDownLoadUrl = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        List<String[]> seresult = new ArrayList<String[]>();
        List<ExportMeetingShowDTO> result = new ArrayList<ExportMeetingShowDTO>();

        try{
            String sql = "SELECT mr.meeting_room_name,mr.meeting_room_desc,at.attachment_addr,mt.meetName,mt.meetdesc,mt.meet_addr,mt.meet_device,mt.meetvolume," +
                    " mr.pre_time_warn,mr.meeting_room_starttime,mr.meeting_room_endtime,km.member_name,mr.create_time " +
                    "from meeting_room mr " +
                    "left JOIN meet_type mt " +
                    "on mt.meet_type_id = mr.meet_type_id " +
                    "left join attachment at " +
                    "on at.attachment_id = mr.attachment_id " +
                    "left join kam_member km " +
                    "on km.member_id = mr.member_id " +
                    "where 1=1 ";

            JSONObject search = new JSONObject(condition);

            if (search.has("starttime")) {
                if(null != search.get("starttime").toString() && !search.get("starttime").toString().isEmpty()){
                    sql += " and mr.create_time >'" + search.get("starttime").toString() + "' ";
                }
            }
            if (search.has("endtime")) {
                if(null != search.get("endtime").toString() && !search.get("endtime").toString().isEmpty()){
                    sql += " and mr.create_time < '" + search.get("endtime").toString() + "' ";
                }
            }
            if (search.has("member_id")) {
                if(null != search.get("member_id").toString() && !search.get("member_id").toString().isEmpty()){
                    sql += "and km.member_id = '" + search.get("member_id").toString() + "' ";
                }
            }
            if (search.has("organization_id")) {
                if(null != search.get("organization_id").toString() && !search.get("organization_id").toString().isEmpty()){
                    sql += "and mt.meet_type_id ='" + search.get("organization_id").toString() + "' ";
                }
            }

            if(search.has("isverify")){
                if(null != search.get("isverify").toString() && !search.get("isverify").toString().isEmpty()){
                    sql += "and mr.isverify ='" + search.get("isverify").toString() + "' ";
                }
            }

            seresult = this.getReportingTo(sql);

            for (int i = 0; i < seresult.size(); i++){
                ExportMeetingShowDTO exportMeetingShowDTO = new ExportMeetingShowDTO();
                exportMeetingShowDTO.setMeeting_room_name(seresult.get(i)[1] == null ? "" :seresult.get(i)[1]);
                exportMeetingShowDTO.setMeeting_room_desc(seresult.get(i)[2] == null ? "" :seresult.get(i)[2] );
                exportMeetingShowDTO.setAttachment_addr(seresult.get(i)[3] == null ? "" :seresult.get(i)[3]);
                exportMeetingShowDTO.setMeetName(seresult.get(i)[4] == null ? "" :seresult.get(i)[4] );
                exportMeetingShowDTO.setMeetdesc(seresult.get(i)[5] == null ? "" :seresult.get(i)[5] );
                exportMeetingShowDTO.setMeet_addr(seresult.get(i)[6] == null ? "" :seresult.get(i)[6] );
                exportMeetingShowDTO.setMeet_device(seresult.get(i)[7] == null ? "" :seresult.get(i)[7] );
                exportMeetingShowDTO.setMeetvolume(seresult.get(i)[8] == null ? "" :seresult.get(i)[8] );
                exportMeetingShowDTO.setPre_time_warn(seresult.get(i)[9] == null ? "" :seresult.get(i)[9] );
                exportMeetingShowDTO.setMeeting_room_starttime(seresult.get(i)[10] == null ? "" :seresult.get(i)[10] );
                exportMeetingShowDTO.setMeeting_room_endtime(seresult.get(i)[11] == null ? "" :seresult.get(i)[11] );
                exportMeetingShowDTO.setMember_name(seresult.get(i)[12] == null ? "" :seresult.get(i)[12] );
                exportMeetingShowDTO.setCreate_time(seresult.get(i)[13] == null ? "" :seresult.get(i)[13] );
                result.add(exportMeetingShowDTO);
            }


            String[] headers = {"会议名称", "会议描述", "会议附件下载地址", "会议室名称", "会议室描述", "会议室地址", "会议室设备", "会议室容量", "会议室使用时长", "会议开始时间","会议结束时间","会议室预订人","会议创建时间"};
            String title = "会议室使用统计";
            String filename = "会议室使用统计表";//+ df.format(new Date())

            ExportExcel<ExportMeetingShowDTO> exportExcel = new ExportExcel<ExportMeetingShowDTO>();
            downloadUrl = exportExcel.createExcelFile(exportExcel, title, headers, result, filename);

            try {
                pdfDownLoadUrl = ExportPDF.createPDFFile(seresult, title, headers, filename);
            } catch (DocumentException e) {
                e.printStackTrace();
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(null != downloadUrl && "" != downloadUrl){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(downloadUrl);
            resultDTO.setStr(pdfDownLoadUrl);
            resultDTO.setValues(result);
        }
        else{
            resultDTO.setStatus("90001");
        }

        return resultDTO;
    }
}
